package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.PageResult;
import com.itheima.domain.pojo.Clazz;
import com.itheima.domain.pojo.ClazzQueruparam;
import com.itheima.mapper.ClazzMapper;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzSetviceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);
    }
    @Override
    public List<Clazz> saveList(){
        return clazzMapper.saveList();
    }
    //分页查询
    @Override
    public PageResult<Clazz> page(ClazzQueruparam clazzQueruparam) {
        //设置分页参数
        PageHelper.startPage(clazzQueruparam.getPage(), clazzQueruparam.getPageSize());
        //直行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueruparam);
        //解析查询结果并封装
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //根据id删除班级信息
    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    //根据id查班级信息
    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    //根据id修改班级信息
    @Override
    public void update(Clazz clazz) {
        //补全基础属性
        clazz.setUpdateTime(LocalDateTime.now());
        //调用mapper接口更新班级信息
        clazzMapper.update(clazz);

    }


}
